module order_send_tb;

    localparam NUM_CONTRACTS = 3;
    logic clk_net;
    logic rst_n;

    logic [31:0] fv_all            [NUM_CONTRACTS];
    logic [31:0] bbo_bidprice      [NUM_CONTRACTS];
    logic [31:0] bbo_askprice      [NUM_CONTRACTS];
    logic        bbo_changed       [NUM_CONTRACTS];

    logic        send_order_vld    [NUM_CONTRACTS];
    logic [31:0] send_order_price  [NUM_CONTRACTS];
    logic        send_order_buy    [NUM_CONTRACTS];

    order_send UUT ( .* );

    // Initialise clock
    initial clk_net = 0;
    always #5 clk_net = ~clk_net;

    integer i;
    initial begin

        $dumpfile("order_send_tb.vcd");
        $dumpvars(0,UUT);
        $dumpvars(0, UUT.send_order_vld[0]);
        $dumpvars(0, UUT.send_order_vld[1]);
        $dumpvars(0, UUT.send_order_vld[2]);
        $dumpvars(0, UUT.send_order_price[0]);
        $dumpvars(0, UUT.send_order_price[1]);
        $dumpvars(0, UUT.send_order_price[2]);
        $dumpvars(0, UUT.send_order_buy[0]);
        $dumpvars(0, UUT.send_order_buy[1]);
        $dumpvars(0, UUT.send_order_buy[2]);       

        // Initialise
        rst_n = 0;
        for (i = 0; i < NUM_CONTRACTS; i++) begin
            bbo_bidprice[i]  = 0;
            bbo_askprice[i]  = 0;
            bbo_changed[i]   = 0;
            fv_all[i]        = 0;
        end

        fv_all[0] = 2529; // Royal Gala
        fv_all[1] = 2554; // Granny Smith
        fv_all[2] = 2539; // Fuji
        
        bbo_bidprice[0] = 2500;
        bbo_askprice[0] = 2505;
        bbo_bidprice[1] = 2520;
        bbo_askprice[1] = 2530;

        #10 rst_n = 1;
        // Example 1 from the task description file
        // BBO for Granny Smith apples is  2 2520 | 2530 3 and Royal Gala change to 13 2500 | 2510 10
        // Should send buy order
        #30;
        bbo_askprice[0] = 2510;
        bbo_bidprice[0] = 2500;
        bbo_changed[0] = 1;
        bbo_changed[1] = 1;
        fv_all[0] = 2506;
        fv_all[1] = 2531;
        fv_all[2] = 2516;

        #20;
        bbo_changed[0] = 0;
        bbo_changed[1] = 0;

        // Example 2 from the task description file
        // BBO for Granny Smith apples is  2 2520 | 2530 3 and change to 1 2530 | 2535 3
        // It should not send any orders
        #30;
        bbo_bidprice[1] = 2530;
        bbo_askprice[1] = 2535;
        bbo_changed[1] = 1;
        fv_all[0] = 2506;
        fv_all[1] = 2531;
        fv_all[2] = 2516;

        #30 bbo_changed[1] = 0;

        // Test example 3
        // BBO for Fuji apples is 2 2520 | 2530 3 
        // Should send sell order
        #50;
        bbo_bidprice[2] = 2530;
        bbo_askprice[2] = 2535;
        bbo_changed[2] = 1;
        fv_all[0] = 2506;
        fv_all[1] = 2531;
        fv_all[2] = 2516;

        #20 bbo_changed[1] = 0;

        // Example 4
        // Fuji: FV 高于 ask -> 应触发买单
        #50;
        bbo_bidprice[2] = 2530;
        bbo_askprice[2] = 2535;
        fv_all[2] = 2538;     // 高于 ask
        bbo_changed[2] = 1;

        #20 bbo_changed[2] = 0;


        // Example 5
        // Granny Smith: FV 刚好等于 ask -> 不应触发
        #50;
        bbo_bidprice[1] = 2529;
        bbo_askprice[1] = 2531;
        fv_all[1] = 2531;     // 等于 ask
        bbo_changed[1] = 1;

        #20 bbo_changed[1] = 0;


        // Example 6
        // Fuji: FV 明显低于 bid -> 应触发卖单
        #30;
        bbo_bidprice[2] = 2535;
        bbo_askprice[2] = 2540;
        fv_all[2] = 2520;     // 低于 bid
        bbo_changed[2] = 1;

        #20 bbo_changed[2] = 0;


        // Example 7
        // Granny Smith: 价差缩小 + FV 高于 ask -> 应触发买单
        #30;
        bbo_bidprice[1] = 2528;
        bbo_askprice[1] = 2531;
        fv_all[1] = 2535;
        bbo_changed[1] = 1;

        #20 bbo_changed[1] = 0;

        // Test negative logic reset
        #50;
        rst_n = 0;

        #10;
        
        $finish;        

    end
        

endmodule